package org.deng.learn.concurrent.basic;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Project
 * @Module
 * @JDK_Vserion 1.8
 * @Author DengLibin
 * @Email dlb@smarthse.cn
 * @Desc 信号量 控制同时访问特定资源的线程数
 * @since 2019/11/7 0007 11:42
 */
public class SemaphoreDemo {

    private static final int THREAD_NUM = 30;

    private static  ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(5, 10,
            1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(2000));

    private static Semaphore semaphore = new Semaphore(5);

    public static void main(String[] args) {

        for(int i = 0; i < THREAD_NUM; i++){
            threadPoolExecutor.execute(()->{
                try {
                    semaphore.acquire();
                    System.out.println("execute");
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            });
        }
        threadPoolExecutor.shutdown();
    }
}
